#! /usr/bin/env python
#coding=utf-8
import web
import PyRSS2Gen
import datetime
from random import randint
import models
from utils import send_to, get_current_theme
import utils
import forms
import widgets

template_env = dict(get_blog_config=models.get_blog_config,
                    get_widgets=widgets.get_enabled_widgets)
render = utils.render('themes/%s/templates' % get_current_theme(), template_env)
rand_range = (37, 34322)

class Home(object):

    @send_to(render, 'index.html')
    def GET(self, start_index=None):
        config = models.get_blog_config()
        posts, prev, next = models.Entry.get_posts(start_index, config.entries_per_page, True)
        links = utils.get_paging_links(posts, prev, next, start_index)
        return dict(entries=posts,
                    links=links,
                    )

         
class RssViewer(object):

    def GET(self):
        config = models.get_blog_config()
        items = []
        entries, extra = models.Entry.get_posts(None, models.Entry.get_count())

        for entry in entries:
            link='http://%s/view/%d' % (web.ctx.host, entry.index)
            items.append(PyRSS2Gen.RSSItem(title=entry.title,
                        link=link,
                        description=entry.content,
                        author=entry.author.nickname(),
                        guid=PyRSS2Gen.Guid(link),
                        pubDate=entry.date))

        rss=PyRSS2Gen.RSS2(title=config.title,
                        link='http://%s' % web.ctx.host,
                        description=config.subtitle,
                        lastBuildDate=datetime.datetime.now(),
                        items=items)

        web.header('Content-Type', 'application/rss+xml; charset=utf-8')
        return rss.to_xml()




class EntryViewer(object):

    @send_to(render, 'entry.html')
    def GET(self, index):
        entry = models.Entry.get_by_index(utils.safeint(index))
        if not entry:
            raise web.notfound()
        randnum = randint(*rand_range)
        return dict(entry=entry,
                    title=entry.title,
                    cmtform=forms.comment_form(randnum))

    @send_to(render, 'entry.html')
    def POST(self, index):
        entry = models.Entry.get_by_index(utils.safeint(index))
        if not entry:
            raise web.notfound()

        randnum = randint(*rand_range)
        cmtform = forms.comment_form(randnum)
        i = web.input()
        # TODO: give friendly feedback if getting the wrong number
        is_spam = forms.comment_is_spam(i)
        i.randnum = randnum  # got to update this value manually...
        if not cmtform.validates(i) or is_spam:
            return dict(entry=entry,
                        title=entry.title,
                        cmtform=cmtform)

        models.Comment.new(entry=entry,
                            content=i.content,
                            author=i.author,
                            site=i.url,
                            email=i.email)

        raise web.seeother(web.ctx.path)


class TagViewer(object):

    @send_to(render, 'tag.html')
    def GET(self, tagname, start_index=None):
        results, prev, next = models.Entry.get_posts(start_index, models.get_blog_config().entries_per_page, True, tagname)
        links = dict(next=0, prev=0)

        if prev:
            links['prev'] = prev.index
        if next:
            links['next'] = next.index

        return dict(entries=results,
                    links=links,
                    tag=tagname,
                    title='view by tag %s' % tagname)
